package CompleteFutureDemo;

import common.CalcTask;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author chao
 * @create 2022-02-06-17:24
 */
public class AllOfTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Double> cf = CompletableFuture.supplyAsync(() -> new CalcTask().RunWithResult());
        CompletableFuture<Double> cf2 = CompletableFuture.supplyAsync(() -> new CalcTask().RunWithResult());
        CompletableFuture<Double> cf3 = CompletableFuture.supplyAsync(() -> new CalcTask().RunWithResult());
        CompletableFuture<Object> cf4 = CompletableFuture.anyOf(cf, cf2, cf3).whenComplete((result, except) -> {
            if (except != null) {
                System.out.println("异常 -> " + except);
                except.printStackTrace();
            } else {
                System.out.println("anyOf 结果是：" + result);
            }
        });
        CompletableFuture<Void> cf5 = CompletableFuture.allOf(cf, cf2, cf3).whenComplete((result, except) -> {
            if (except != null) {
                System.out.println("异常 -> " + except);
                except.printStackTrace();
            } else {
                System.out.println("allOf 结果是：" + result);
            }
        });
        cf4.get();
        cf5.get();
    }
}
